var common = require('../../utils/common.js');
var address = require('../../utils/city.js');
var that ='';
Page({
  data: {
    showsearch: false,   //显示搜索按钮
    searchtext: '',  //搜索文字
    filterdata: {},  //筛选条件数据
    showfilter: false, //是否显示下拉筛选
    showfilterindex: null, //显示哪个筛选类目
    sortindex: 0,  //一级分类索引
    sortid: null,  //一级分类id
    subsortindex: 0, //二级分类索引
    subsortid: null, //二级分类id
    cityindex: 0,  //一级城市索引
    cityid: null,  //一级城市id
    subcityindex: 0,  //二级城市索引
    subcityid: null, //二级城市id
    housetypeindex:0,//户型索引
    housetypeid:0,//户型id
    moreindex:0,//更多索引
    moreid: 0,//更多id
    servicelist: [], //服务集市列表
    scrolltop: false, //滚动位置
    page: 1,  //分页
    locationName:'位置',
    //
    latitude:"",
    longitude:"",
  },
  onLoad: function () { //加载数据渲染页面
    // this.fetchServiceData();
    that = this;
    this.fetchFilterData();
    this.initLocation();
  },
  initLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        console.log('latitude' + latitude + ' longitude' + longitude)
        that.fetchServiceData();
      }
    })

  },
  fetchFilterData: function () { //获取筛选条件
    // 默认选择南宁
    var id = address.provinces[19].id
    // this.setData({
    //   areas: address.areas[address.citys[id][0].id],
    // })
    this.setData({
      filterdata: {
        "location": [
          {
            "id": 0,
            "title": "全部"
          },
          {
            "id": 1,
            "title": "位置",
            "subsort": address.areas[address.citys[id][0].id],
          }
        ],
        "price": [
          {
            "id": 0,
            "title": "全部"
          },
          {
            "id": 1,
            "title": "600元以下",
            "subcity": [],
          },
          {
            "id": 2,
            "title": "600-1000元",
            "subcity": [],
          },
          {
            "id": 3,
            "title": "1000-1500元",
            "subcity": [],
          },
          {
            "id": 4,
            "title": "1500元以上",
            "subcity": [],
          }
        ],
        "houseType": [
          {
            "id": 0,
            "title": "全部"
          },
          {
            "id": 1,
            "title": "单间配套",
          },
          {
            "id": 2,
            "title": "一房一厅",
          },
          {
            "id": 3,
            "title": "两房一厅",
          },
          {
            "id": 4,
            "title": "三房一厅",
          },
          {
            "id": 5,
            "title": "单层公寓",
          },
          {
            "id": 6,
            "title": "双层公寓",
          }
        ],
        "more": [
          {
            "id": 0,
            "title": "不限"
          },
          {
            "id": 1,
            "title": "距我最近"
          },
          {
            "id": 2,
            "title": "租金低到高",
          },
          {
            "id": 3,
            "title": "租金高到低",
          }
        ]
      }
    })
  },
  fetchServiceData: function () {  //房屋列表
    let _this = this;
    console.log('数据请求')
    common._get('/wx/m/index', { lat: this.data.latitude, lon: this.data.longitude, page: this.data.page },
      //成功
      function (res) {
        if (res.data.success) {
          console.log('数据返回')
          console.log(res.data)
          for (var i in res.data.data.list) {
            res.data.data.list[i].furniture = JSON.parse(res.data.data.list[i].furniture);
          }
         
          _this.setData({
            servicelist: _this.data.servicelist.concat(res.data.data.list)
          })
          _this.data.page++;
          console.log(_this.data.servicelist)
        }
      });
    // setTimeout(() => {
    //   _this.setData({
    //     servicelist: _this.data.servicelist.concat(newlist)
    //   })
    // }, 1500)
  },
  inputSearch: function (e) {  //输入搜索文字
    this.setData({
      showsearch: e.detail.cursor > 0,
      searchtext: e.detail.value
    })
  },
  submitSearch: function () {  //提交搜索
    console.log(this.data.searchtext);
    this.fetchServiceData();
  },
  setFilterPanel: function (e) { //展开筛选面板
    const d = this.data;
    const i = e.currentTarget.dataset.findex;
    if (d.showfilterindex == i) {
      this.setData({
        showfilter: false,
        showfilterindex: null
      })
    } else {
      this.setData({
        showfilter: true,
        showfilterindex: i,
      })
    }
    console.log(d.showfilterindex);
  },
  setSortIndex: function (e) { //位置一级
    const d = this.data;
    const dataset = e.currentTarget.dataset;
    this.setData({
      sortindex: dataset.sortindex,
      sortid: dataset.sortid,
      subsortindex: d.sortindex == dataset.sortindex ? d.subsortindex : 0
    })
    if (dataset.sortindex == 0) {
      this.setData({
        locationName: "位置",
      })
      this.reInitData();
    }
    console.log('服务类别id：一级--' + this.data.sortid + ',二级--' + this.data.subsortid);
  },
  setSubsortIndex: function (e) { //位置二级索引
    
    const dataset = e.currentTarget.dataset;
    this.setData({
      subsortindex: dataset.subsortindex,
      subsortid: dataset.subsortid,
      locationName: dataset.name
    })
    console.log('服务类别id：一级--' + this.data.sortid + ',二级--' + this.data.subsortid);
    this.reInitData();
  },
  // setCityIndex: function (e) { //租金一级索引（s暂时不使用）
  //   const d = this.data;
  //   const dataset = e.currentTarget.dataset;
  //   this.setData({
  //     cityindex: dataset.cityindex,
  //     cityid: dataset.cityid,
  //     subcityindex: d.cityindex == dataset.cityindex ? d.subcityindex : 0
  //   })
  //   console.log('服务城市id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
  // },
  setSubcityIndex: function (e) { //租金索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      subcityindex: dataset.subcityindex,
      subcityid: dataset.subcityid,
    })
    console.log('服务城市id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
    this.reInitData();
  },
  setHouseTypeIndex: function (e) { //户型索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      housetypeindex: dataset.subcityindex,
      housetypeid: dataset.subcityid,
    })
    console.log('服务城市id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
    this.reInitData();
  },
  setMoreIndex: function (e) { //更多
    const dataset = e.currentTarget.dataset;
    this.setData({
      moreindex: dataset.subcityindex,
      moreid: dataset.subcityid,
    })
    console.log('服务城市id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
    this.reInitData();
  },
  reInitData:function(){//重新获取列表数据
    this.hideFilter();
    this.setData({
      page:1,
      servicelist:[],
    })
    this.fetchServiceData();
  },
  hideFilter: function () { //关闭筛选面板
    this.setData({
      showfilter: false,
      showfilterindex: null
    })
  },
  scrollHandle: function (e) { //滚动事件
    var g = e.detail.scrollTop;
    if (!this.data.scrolltop) {
      if (g > 200) {
        this.setData({
          scrolltop: true,
        })
      }
    } else {
      if (g < 200) {
        this.setData({
          scrolltop: false,
        })
      }
    }

  },
  goToTop: function () { //回到顶部
    this.setData({
      scrolltop: 0
    })
  },
  scrollLoading: function () { //滚动加载
    this.fetchServiceData();
  },
  onPullDownRefresh: function () { //下拉刷新
    this.setData({
      page: 1,
      servicelist: [],
    })
    this.fetchServiceData();
    this.fetchFilterData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉获取跟多数据
    //   this.setData({
    //     scrolltop: 300,
    //   })
    //   this.fetchServiceData();
    },
  clickSearch: function (e) {
    // onsole.log(11)
    // if (app.globalData.config.hasPermission) {
    wx.navigateTo({
      url: '../search/search',
    })
    // }
  },
  })